package cn.iocoder.yudao.module.reservation.service.promotionactivity;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.reservation.controller.admin.promotionactivity.vo.PromotionActivityPageReqVO;
import cn.iocoder.yudao.module.reservation.controller.admin.promotionactivity.vo.PromotionActivitySaveReqVO;
import cn.iocoder.yudao.module.reservation.dal.dataobject.promotionactivity.PromotionActivityDO;
import cn.iocoder.yudao.module.reservation.dal.mysql.promotionactivity.PromotionActivityMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.PROMOTION_ACTIVITY_NOT_EXISTS;

/**
 * 优惠活动 Service 实现类
 *
 * @author yy
 */
@Service
@Validated
public class PromotionActivityServiceImpl implements PromotionActivityService {

    @Resource
    private PromotionActivityMapper promotionActivityMapper;

    @Override
    public Long createPromotionActivity(PromotionActivitySaveReqVO createReqVO) {
        // 插入
        PromotionActivityDO promotionActivity = BeanUtils.toBean(createReqVO, PromotionActivityDO.class);
        promotionActivityMapper.insert(promotionActivity);
        // 返回
        return promotionActivity.getId();
    }

    @Override
    public void updatePromotionActivity(PromotionActivitySaveReqVO updateReqVO) {
        // 校验存在
        validatePromotionActivityExists(updateReqVO.getId());
        // 更新
        PromotionActivityDO updateObj = BeanUtils.toBean(updateReqVO, PromotionActivityDO.class);
        promotionActivityMapper.updateById(updateObj);
    }

    @Override
    public void deletePromotionActivity(Long id) {
        // 校验存在
        validatePromotionActivityExists(id);
        // 删除
        promotionActivityMapper.deleteById(id);
    }

    private void validatePromotionActivityExists(Long id) {
        if (promotionActivityMapper.selectById(id) == null) {
            throw exception(PROMOTION_ACTIVITY_NOT_EXISTS);
        }
    }

    @Override
    public PromotionActivityDO getPromotionActivity(Long id) {
        return promotionActivityMapper.selectById(id);
    }

    @Override
    public PageResult<PromotionActivityDO> getPromotionActivityPage(PromotionActivityPageReqVO pageReqVO) {
        return promotionActivityMapper.selectPage(pageReqVO);
    }

}